#encoding:utf-8
from django.shortcuts import render, redirect, render_to_response
from django.contrib.auth import authenticate, login
from django.template import RequestContext
import companies
from companies.models import Company_Area, Company_Industry
from companies.forms import CompanyForm
from common.forms import *

from userprofiles import settings as up_settings
from userprofiles.utils import get_form_class
from userprofiles.forms import RegistrationForm

from django.utils.translation import ugettext as _

from countries_states.models import *


def registration(request, profile):
    pageheader = ''
    if profile == 'company':
        # RegistrationForm = get_form_class(up_settings.REGISTRATION_FORM_COMPANY)
        # RegistrationForm = RegistrationForm()
        pageheader = _(u'REGISTRO DE EMPRESA')
    elif profile == 'candidate':
        # RegistrationForm = get_form_class(up_settings.REGISTRATION_FORM_CANDIDATE)
        pageheader = _(u'REGISTRO DE CANDIDATO')

    if request.method == 'POST':
        form_user = RegistrationForm(data=request.POST,
                                files=request.FILES,)
        try:
            country_selected = Country.objects.get(id=request.POST['country'])
        except:
            country_selected = None
        try:
            if int(request.POST['state']) == 0:
                state_selected = 0
            else:
                state_selected = State.objects.get(id=request.POST['state'])
        except:
            state_selected = None
        form_address = AdressForm(data=request.POST,
                                  files=request.FILES,
                                  country_selected=country_selected,
                                  state_selected=state_selected,
        )
        try:
            company_area_selected = Company_Area.objects.get(pk=int(request.POST['company_area']))
        except:
            company_area_selected = None
        try:
            if int(request.POST['company_industry']) == 0:
                company_industry_selected = 0
            else:
                company_industry_selected = Company_Industry.objects.get(id=request.POST['company_industry'])
        except:
            company_industry_selected = None
        form_company = CompanyForm(data=request.POST,
                                  files=request.FILES,
                                  company_area_selected=company_area_selected,
                                  company_industry_selected=company_industry_selected,
        )
        if form_user.is_valid() and form_company.is_valid() and form_address.is_valid():
            new_user = form_user.save()
            new_address = form_address.save(commit=False)
            new_company = form_company.save(new_user=new_user, address=new_address, commit=False)
            new_address.save()
            new_company.save()
            form_user.send_verification_mail(new_user)
            # new_user.send_verification_mail(new_user=new_user)
            username = form_user.cleaned_data['username']
            password = form_user.cleaned_data['password']
            # Automatically log this user in
            if up_settings.AUTO_LOGIN:
                if up_settings.EMAIL_ONLY:
                    username = form_user.cleaned_data['email']
                user = authenticate(username=username, password=password)
                if user is not None:
                    if user.is_active:
                        login(request, user)
            return redirect(up_settings.REGISTRATION_REDIRECT)
    else:
        form_user = RegistrationForm()
        form_address = AdressForm()
        form_company = CompanyForm()

    return render_to_response('userprofiles/registration.html',
                              {'form_user': form_user,
                               'form_address': form_address,
                               'form_company': form_company,
                               'pageheader': pageheader,
                               'registration': True},
                              context_instance=RequestContext(request))


def registration_complete(request):
    return render(request, 'userprofiles/registration_complete.html', {
        'account_verification_active': up_settings.USE_ACCOUNT_VERIFICATION,
        'expiration_days': up_settings.ACCOUNT_VERIFICATION_DAYS,
    })
